package org.fxml.containers {
	import mx.containers.BoxDirection;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.text.TextField;

	/**
	 * @author jordandoczy
	 */
	public class Box extends Sprite implements IBox {
		
		protected var _adjustment:uint = 0;
		protected var _direction:String;
		protected var _items:Array = new Array();
		protected var _padding:Number = 0;

		public function get direction():String{
			return _direction;
		}
		
		public function set direction(value:String):void{
			_direction = value;
		}
		
		public function get padding():Number{
			return _padding;
		}
		
		public function set padding(value:Number):void{
			_padding = value;
		}
		
		public override function addChild(content:DisplayObject):DisplayObject{
			
			if(_direction == BoxDirection.HORIZONTAL){
				content.x = _adjustment;
				if(content is TextField) _adjustment += TextField(content).textWidth + _padding;
				else _adjustment += content.width + _padding;	
			}
			else{ // VERTICAL
				content.y = _adjustment;
				if(content is TextField) _adjustment += TextField(content).textHeight + _padding;
				else _adjustment += content.height + _padding;	
			}
			
			_items.push(content);
			
			return super.addChild(content);
		}
		
		public function clear():void{
			var child:DisplayObject;
			for each (child in _items){
				removeChild(child);
			}
			_items = new Array();
			_adjustment = 0;
		}
	}
}
